package com.example.innovate.controller;

import com.example.innovate.bean.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

@RestController
@RequestMapping("/api")
@Api(tags = "下载接口")
public class DownLoadController {

//    //@UserLoginToken
//    @RequestMapping(value = "/download", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
//    @ApiOperation("下载")
//    public void downLoad(@RequestParam("fileName") String fileName, HttpServletResponse response) throws UnsupportedEncodingException {
//        String filePath = "/root/file/"; // C:\\file\\
//        // 如果文件名不为空，则进行下载
//        if (fileName != null) {
//            // File file = new File(filepath);
//            File file = new File(filePath + fileName);
//            // 如果文件存在，则进行下载
//            System.out.println(file);
//            if (file.exists()) {
//                // 配置文件下载
//                response.setHeader("content-type", "application/octet-stream");
//                response.setContentType("application/octet-stream;charset=utf-8");
//                // 下载文件能正常显示中文
//                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
//                // 实现文件下载
//                byte[] buffer = new byte[1024];
//                FileInputStream fis = null;
//                BufferedInputStream bis = null;
//                try {
//                    fis = new FileInputStream(file);
//                    bis = new BufferedInputStream(fis);
//                    OutputStream os = response.getOutputStream();
//                    int i = bis.read(buffer);
//                    while (i != -1) {
//                        os.write(buffer, 0, i);
//                        i = bis.read(buffer);
//                    }
//                    //return new Result(200,"success","下载成功",fileName);
//
//                } catch (Exception e) {
//                    //return new Result(400,"error","下载失败",fileName);
//
//                } finally {
//                    if (bis != null) {
//                        try {
//                            bis.close();
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                    if (fis != null) {
//                        try {
//                            fis.close();
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            }
//        }
//        //return new Result(0,"","",null);
//    }

//    //@PassToken
//    @RequestMapping(value = "/image", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
//    @ApiOperation("图片")
//    public void image(@RequestParam("fileName") String fileName, HttpServletResponse response) throws UnsupportedEncodingException {
//        String filePath = "/root/file/" ;
//        // 如果文件名不为空，则进行下载
//        if (fileName != null) {
//            //获取文件的后缀名
//            String suffix = fileName.substring(fileName.lastIndexOf("."));
//            if(suffix.equals(".jpeg")||suffix.equals(".jpg")||suffix.equals(".png")){
//                File file = new File(filePath + fileName);
//                // 如果文件存在，则进行下载
//                if (file.exists()) {
//                    // 配置文件下载
//                    response.setHeader("content-type", "application/octet-stream");
//                    response.setContentType("application/octet-stream");
//                    // 下载文件能正常显示中文
//                    response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
//                    // 实现文件下载
//                    byte[] buffer = new byte[1024];
//                    FileInputStream fis = null;
//                    BufferedInputStream bis = null;
//                    try {
//                        fis = new FileInputStream(file);
//                        bis = new BufferedInputStream(fis);
//                        OutputStream os = response.getOutputStream();
//                        int i = bis.read(buffer);
//                        while (i != -1) {
//                            os.write(buffer, 0, i);
//                            i = bis.read(buffer);
//                        }
////                        return new Result(200,"success","下载成功",fileName);
//
//                    } catch (Exception e) {
////                        return new Result(400,"error","下载失败",fileName);
//
//                    } finally {
//                        if (bis != null) {
//                            try {
//                                bis.close();
//                            } catch (IOException e) {
//                                e.printStackTrace();
//                            }
//                        }
//                        if (fis != null) {
//                            try {
//                                fis.close();
//                            } catch (IOException e) {
//                                e.printStackTrace();
//                            }
//                        }
//                    }
//                }
//            }
//            else{
////                return new Result(0,"","非图片格式",null);
//            }
//
//        }
////        return new Result(0,"","",null);
//    }

    //@UserLoginToken
    @RequestMapping(value = "/download", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation("下载")
    public Result downLoad(@RequestParam("fileName") String fileName, HttpServletResponse response) throws UnsupportedEncodingException {
        String filePath = "/root/file/";
        // 如果文件名不为空，则进行下载
        if (fileName != null) {
            File file = new File(filePath + fileName);
            // 如果文件存在，则进行下载
            if (file.exists()) {
                String s = "http://42.193.219.136:8080/images/"+fileName;
                return new Result(200,"success","下载成功",s);
            }
            else{
                return new Result(400,"error","文件不存在",null);
            }
        }
        return new Result(0,"","格式错误",null);
    }

    //@PassToken
    @RequestMapping(value = "/image", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation("图片")
    public Result image(@RequestParam("fileName") String fileName, HttpServletResponse response) throws UnsupportedEncodingException {
        String filePath = "/root/file/" ;
        // 如果文件名不为空，则进行下载
        if (fileName != null) {
            //获取文件的后缀名
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            if(suffix.equals(".jpeg")||suffix.equals(".jpg")||suffix.equals(".png")){
                File file = new File(filePath + fileName);
                // 如果文件存在，则进行下载
                if (file.exists()) {
                    String s = "http://42.193.219.136:8080/images/"+fileName;
                    return new Result(200,"success","返回成功",s);
                }
                else{
                    return new Result(400,"error","文件不存在",null);
                }
            }
            else{
                return new Result(0,"","非图片格式",null);
            }

        }
        return new Result(0,"","",null);
    }

    //@UserLoginToken
    @PostMapping(value = "/deleteFile")
    @ApiOperation("删除")
    public Result delete(@RequestParam("fileName") String fileName){
        String filePath = "/root/file/" ;
        File file = new File(filePath +  fileName);
        if (file.isFile() && file.exists()) {
            file.delete();
        }
        return new Result(200,"success","删除成功",fileName);
    }

}
